/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import com.andkulikov.transitionseverywhere.slice.util.ResUtil;
import com.andkulikov.transitionseverywhere.ResourceTable;
import com.transitionseverywhere.Transition;
import com.transitionseverywhere.TransitionManager;
import com.transitionseverywhere.VisibleToggleClickListener;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.utils.LightweightMap;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class AutoTransitionSlice extends AbilitySlice {
    private DependentLayout mDlViewRoot;
    private Text mTxtAutoTrans;
    private Timer mDelayTimer = new Timer();
    Transition trans;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(this).
                parse(ResourceTable.Layout_slice_auto_transition, null, false);
        mDlViewRoot = (DependentLayout) layout.findComponentById(com.andkulikov.transitionseverywhere.ResourceTable.Id_dlViewRoot);
        Object tagMap = new LightweightMap<String, ArrayList<Transition>>();
        mDlViewRoot.setTag(tagMap);
        mTxtAutoTrans = (Text) layout.findComponentById(ResourceTable.Id_txtAutoTrans);
        Button btnDoMagic = (Button) layout.findComponentById(ResourceTable.Id_btnDoMagic);
        ShapeElement background_color_element = new ShapeElement();
        background_color_element.setShape(ShapeElement.RECTANGLE);
        background_color_element.setRgbColor(new RgbColor(ResUtil.getColor(this, ResourceTable.Color_red)));
        background_color_element.setBounds(0, 0, btnDoMagic.getRight(), btnDoMagic.getBottom());
        btnDoMagic.setBackground(background_color_element);
        btnDoMagic.setClickedListener(new VisibleToggleClickListener() {
            @Override
            protected void changeVisibility(boolean visible) {
                TransitionManager.beginDelayedTransition(mDlViewRoot);
                if(visible){
                    mTxtAutoTrans.setVisibility(visible ? Component.VISIBLE : Component.INVISIBLE);
                }else {

                    TaskDispatcher dispatcher = getUITaskDispatcher();
                    dispatcher.delayDispatch(() -> mTxtAutoTrans.setVisibility(visible ? Component.VISIBLE : Component.INVISIBLE), 10);
                }
            }
        });
        super.setUIContent(layout);
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        //super.onBackPressed();
        terminate();
    }
}